package com.gxwy.shenzhou.message;

import com.gxwy.shenzhou.HeaderRequest;
import com.gxwy.shenzhou.SplitString;
import com.gxwy.shenzhou.StringHandler;
import com.gxwy.shenzhou.Supply;


public class HuoQiZhuanZhengCunZhengQuKaiHuResponse implements StringHandler{
private String xm;
private String xxh;
private String zhdh;
private String hbzl;


private HeaderRequest header=new HeaderRequest(true);
private static final int XM_len=60;
private static final int XXH_len=3;
private static final int ZHDH_len=32;
private static final int HBZL_len=2;

private static final int XM_FLAG=1;
private static final int XXH_FLAG=1;
private static final int ZHDH_FLAG=1;
private static final int HBZL_FLAG=1;
private int reallon=0;
private static final int Total=XM_len+XXH_len+ZHDH_len+HBZL_len;

public HeaderRequest getHeader() {
	return header;
}
public void setHeader(HeaderRequest header) {
	this.header = header;
}
public String getXm() {
	return xm;
}
public void setXm(String xm) throws Exception{
	if(xm==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(xm.getBytes("gbk").length<XM_len){
    
		this.xm=Supply.supply(xm,XM_FLAG,XM_len);
	}else if(xm.getBytes("gbk").length>XM_len){
		 Exception e=new Exception("Too Long");
		 throw e;
	}else{
		this.xm = xm;			
	}
}

public String getXxh() {
	return xxh;
}
public void setXxh(String xxh) throws Exception {
	if(xxh==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(xxh.getBytes("gbk").length<XXH_len){
	 
		this.xxh=Supply.supply(xxh,XXH_FLAG, XXH_len);
	}else if(xxh.getBytes("gbk").length>XXH_len){
		 Exception e=new Exception("Too Long");
		 throw e;
	}else{
		this.xxh = xxh;			
	}
}
public String getZhdh() {
	return zhdh;
}
public void setZhdh(String zhdh) throws Exception{
	if(zhdh==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(zhdh.getBytes("gbk").length<ZHDH_len){
   
		this.zhdh=Supply.supply(zhdh, ZHDH_FLAG, ZHDH_len);
	}else if(zhdh.getBytes("gbk").length>ZHDH_len){
		 Exception e=new Exception("Too Long");
		 throw e;
	}else{
		this.zhdh = zhdh;			
	}
}
public String getHbzl() {
	return hbzl;
}
public void setHbzl(String hbzl) throws Exception{
	if(hbzl==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(hbzl.getBytes("gbk").length<HBZL_len){
   		this.hbzl=Supply.supply(hbzl,HBZL_FLAG, HBZL_len);
	}else if(hbzl.getBytes("gbk").length>HBZL_len){
		 Exception e=new Exception("Too Long");
		 throw e;
	}else{
		this.hbzl= hbzl;			
	}
}
public String build () throws Exception{
	if(this.getHeader()==null){
		throw new Exception("Header is null");
	}
	if(this.getXm()==null){
		this.setXm("");
	}
    if(this.getXxh()==null){
    	this.setXxh("");
    }
    if(this.getZhdh()==null){
    	this.setZhdh("");
    }
    if(this.getHbzl()==null){
    	this.setHbzl("");
    }
    Integer totallen=header.TOTAL_LEN+Total-HeaderRequest.LEN_LEN;
	this.getHeader().setLen(totallen.toString());
  return this.getHeader().build()+ this.getXm()+this.getXxh()+this.getZhdh()+this.getHbzl();
		}
public void parse(String str) throws Exception{
	if(str==null){
		Exception e=new Exception("String is null");
		throw e;
	}else if(str.getBytes("gbk").length<Total+header.TOTAL_LEN){
		Exception e=new Exception("Invalid request");
		throw e;
		}else{
		String headerLen=SplitString.splitString(str,reallon, reallon+header.TOTAL_LEN);
	
		reallon+=header.TOTAL_LEN;
		header.parse(headerLen);
	 this.setXm(SplitString.splitString(str,reallon,reallon+XM_len));
	 reallon+=XM_len;
	 this.setXxh(SplitString.splitString(str,reallon,reallon+XXH_len));
	 reallon+=XXH_len;
	this.setZhdh(SplitString.splitString(str,reallon,reallon+ZHDH_len));
	reallon+=ZHDH_len;
	this.setHbzl(SplitString.splitString(str,reallon,reallon+HBZL_len));
	
	}

}
public static void main(String args[]) throws Exception{
	HuoQiZhuanZhengCunZhengQuKaiHuResponse a=new HuoQiZhuanZhengCunZhengQuKaiHuResponse();
	
	a.setHbzl("1");
	a.setXm("6");
	a.setXxh("5");
	a.setZhdh("3");

	a.parse(a.build());
	if(a.getHbzl().trim().equals("1")){
		System.out.println("HBZLyes");
	}
	if(a.getXm().trim().equals("6")){
		System.out.println("Xmyes");
	}
	if(a.getXxh().trim().equals("5")){
		System.out.println("XXHȷyes");
	}
	if(a.getZhdh().trim().equals("3")){
		System.out.println("zhdhȷyes");
	}
	System.out.println(a.getHbzl());
	System.out.println(a.getXm());
	System.out.println(a.getXxh());
	System.out.println(a.getZhdh());
}


}
